<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.util.List" %>
<%@ page import="java.io.File" %>
<%@ page import="java.util.UUID" %><%--
  Created by IntelliJ IDEA.
  User: oldliu
  Date: 2023-04-10
  Time: 17:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    //文件暂存的位置
    DiskFileItemFactory df = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(df);
    upload.setHeaderEncoding("utf-8");//设置字符集
    List<FileItem> fileItems = upload.parseRequest(request);//解析request
    for (FileItem f : fileItems) {
        if (f.isFormField()) {
            out.print("<p>");
            out.print("名字："+f.getFieldName()+",值："+f.getString("utf-8"));
            out.print("</p>");
            continue;
        }
        if(f.getSize()<1)
            continue;
        out.print("文件名："+f.getName()+",大小："+f.getSize());
        String path=application.getRealPath("images");
        File file=new File(path);
        if(file.exists()==false)
            file.mkdirs();
        //防止重名
        String houzhui=f.getName().substring(f.getName().lastIndexOf('.'));
        String newName= UUID.randomUUID()+houzhui;
        f.write(new File(file,newName));
        out.print("图片保存到了"+path+"位置");
        out.print("<img height='300px' width='200px' src='images/"+newName+"'/>");
    }%>
<%--<%
    out.print(request.getParameter("sname"));
    out.print("<br>");
    ServletInputStream in = request.getInputStream();
    int ch=0;
    while ((ch=in.read())!=-1){
        out.print((char)ch);
    }
%>--%>
</body>
</html>
